package com.lightbend.akka.ddata

import akka.actor.{ActorSystem, Props}
import akka.cluster.Cluster
import akka.cluster.ddata.Replicator._
import akka.cluster.ddata._
import com.lightbend.akka.ddata.ReplicatedCache._

object Main extends App {

  implicit val system: ActorSystem = ActorSystem("DDataSystem")

  val cluster = Cluster(system)
  val replicatedCache = system.actorOf(ReplicatedCache.props)

  val probe = system.actorOf(Props.create(classOf[Receiver]))

  // 获取副本个数
  DistributedData(system).replicator.tell(GetReplicaCount, probe)

  replicatedCache ! PutInCache("key1", "A")
  replicatedCache.tell(GetFromCache("key1"), probe)

  for (i ← 100 to 200) {
    replicatedCache ! PutInCache("key" + i, i)
    replicatedCache.tell(GetFromCache("key" + i), probe)
  }

  replicatedCache ! PutInCache("key2", "B")
  replicatedCache.tell(GetFromCache("key2"), probe)

  replicatedCache ! Evict("key2")
}

